package com.lin.carbon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.carbon.entity.SysDictItem;
import com.lin.carbon.entity.SysDictItemVO;
import com.lin.carbon.mapper.SysDictItemMapper;
import com.lin.carbon.service.SysDictItemService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 系统字典条目业务实现类
 */
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    private final SysDictItemMapper sysDictItemMapper;

    @Autowired
    public SysDictItemServiceImpl(SysDictItemMapper sysDictItemMapper) {
        this.sysDictItemMapper = sysDictItemMapper;
    }

    @Override
    public Page<SysDictItemVO> getAllDictItems(int pageNum, int pageSize) {
        Page<SysDictItemVO> page = new Page<>(pageNum, pageSize);
        return sysDictItemMapper.selectDictItemWithName(page);
    }

    @Override
    public SysDictItemVO getDictItemById(Long id) {
        return sysDictItemMapper.selectDictItemWithNameById(id);
    }

    @Override
    public List<Map<String, Object>> selectNameListByDictCode(String dictCode) {
        return sysDictItemMapper.selectNameListByDictCode(dictCode);
    }

    @Override
    public Page<SysDictItemVO> getDictItemPageListByDictCode(Integer pageNum, Integer pageSize, String dictCode) {
        Page<SysDictItemVO> page = new Page<>(pageNum, pageSize);
        return sysDictItemMapper.selectDictItemPageListByDictCode(page, dictCode);
    }


    @Override
    public boolean addDictItem(SysDictItemVO sysDictItemVO) {
        // 设置创建时间和更新时间
        sysDictItemVO.setCreatedTime(LocalDateTime.now());
        sysDictItemVO.setUpdatedTime(LocalDateTime.now());
        return sysDictItemMapper.insert(sysDictItemVO) > 0;
    }
    @Override
    public boolean updateDictItem(SysDictItemVO sysDictItemVO) {
        // 设置更新时间
        sysDictItemVO.setUpdatedTime(LocalDateTime.now());
        return sysDictItemMapper.updateDictItem(sysDictItemVO) > 0;
    }

    @Override
    public boolean deleteDictItemById(Long id) {
        return sysDictItemMapper.deleteDictItemById(id) > 0;
    }

    @Override
    @Transactional(readOnly = true) // 查询操作可使用只读事务
    public List<SysDictItemVO> getCityDict() {
        return sysDictItemMapper.getCityDict();
    }

    @Override
    @Transactional(readOnly = true) // 查询操作可使用只读事务
    public List<SysDictItemVO> getFieldDict() {
        return sysDictItemMapper.getFieldDict();
    }
}